//-------------------------------------------------------------------------------------------------
//ToolsOptionsDlg.cpp : Options Dialog   
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Oct 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------


#include "stdafx.h"
#include "ArchiveProcGUI.h"
#include "ToolsOptionsDlg.h"
#include "afxdialogex.h"


// ToolsOptionsDlg dialog

IMPLEMENT_DYNAMIC(ToolsOptionsDlg, CDialog)

ToolsOptionsDlg::ToolsOptionsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(ToolsOptionsDlg::IDD, pParent)
	, m_RegPath(_T(""))
{
	m_nAutoFileSavePeriod = 0;
	m_DbLocalConnection = _T("");
	m_DbRemoteConnection = _T("");
}

ToolsOptionsDlg::~ToolsOptionsDlg()
{
}

void ToolsOptionsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_REGPATH_STATIC, m_RegPath);
	DDX_Text(pDX, IDC_AUTOFILESAVEPERIODEDIT, m_nAutoFileSavePeriod);
	DDV_MinMaxInt(pDX, m_nAutoFileSavePeriod, 0, 144000);
	DDX_Control(pDX, IDC_AUTOFILESAVEPERIODEDIT, m_AutoSavePeriodCtrl);
	DDX_Text(pDX, IDC_DBLOCAL_CONNECTSTR, m_DbLocalConnection);
	DDV_MaxChars(pDX, m_DbLocalConnection, 5000);
	DDX_Text(pDX, IDC_DBREMOTE_CONNECTSTR, m_DbRemoteConnection);
	DDV_MaxChars(pDX, m_DbRemoteConnection, 5000);
}


BEGIN_MESSAGE_MAP(ToolsOptionsDlg, CDialog)
ON_BN_CLICKED(IDC_RESTOREFACTORYSETTINGSBUTTON, &ToolsOptionsDlg::OnClickedRestoreFactorySettingsButton)
END_MESSAGE_MAP()


// ToolsOptionsDlg message handlers


void ToolsOptionsDlg::OnClickedRestoreFactorySettingsButton()
{
	CWinAppEx *pApp = dynamic_cast<CWinAppEx *>(AfxGetApp());
	if (pApp)
	{
		if ( pApp->CleanState() == TRUE )
		{
			CArchiveProcGUIApp *pMyApp = dynamic_cast< CArchiveProcGUIApp *>(AfxGetApp());
			if (pMyApp)
			{
				m_DbRemoteConnection = "";
				m_DbLocalConnection = "";

				UpdateData(FALSE);

				pMyApp->RestoreFactorySettings();

				CArchiveProcGUIApp::UserMsg("Factory settings restored", "You must now restart this application", MB_OK | MB_ICONSTOP );
			}
			else
				CArchiveProcGUIApp::UserErrorMsg("Fatal Error", "Failed to restore factory settings. Unable to access app", "successful", "Contact support", MB_OK | MB_ICONSTOP );
		}
		else
			CArchiveProcGUIApp::UserErrorMsg("Fatal Error", "Failed to restore factory settings. Unable to access registry", "successful", "Contact support", MB_OK | MB_ICONSTOP );
	}
}


BOOL ToolsOptionsDlg::OnInitDialog()
{
	CWinAppEx *pApp = dynamic_cast<CWinAppEx *>(AfxGetApp());
	if (pApp)
		m_RegPath = pApp->GetRegSectionPath();

	CDialog::OnInitDialog();

	CArchiveProcGUIApp *pMyApp = dynamic_cast<CArchiveProcGUIApp *>(AfxGetApp());
	if (pMyApp)
		m_AutoSavePeriodCtrl.EnableWindow((pMyApp->IsAutoSaveEnabled() == true) ? TRUE : FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}



//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------







